<?php
// +----------------------------------------------------------------------
// | 供应链
// +----------------------------------------------------------------------
namespace app\schain\controller;

use app\admin\model\ThemeModel;
use app\businesscard\model\BusinessCategoryModel;
use app\schain\model\SchainEnterpriseModel;
use app\schain\service\EnterpriseService;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminEnterpriseController 企业管理控制器
 * @package app\schain\controller
 */
class AdminEnterpriseController extends AdminBaseController {
	/**
	 * 企业列表
	 * @adminMenu(
	 *     'name'   => '企业管理',
	 *     'parent' => 'schain/AdminIndex/default',
	 *     'display'=> true,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业列表',
	 *     'param'  => ''
	 * )
	 */
	public function index() {
		$param = $this->request->param();

		$categoryId = $this->request->param('category', 0, 'intval');

		$enterpriseService = new EnterpriseService();
		$data = $enterpriseService->adminEnterpriseList($param);

		$data->appends($param);

		$schainCategoryModel = new BusinessCategoryModel();
		$categoryTree = $schainCategoryModel->adminCategoryTree($categoryId);

		$this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
		$this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
		$this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
		$this->assign('articles', $data->items());
		$this->assign('category_tree', $categoryTree);
		$this->assign('category', $categoryId);
		$this->assign('page', $data->render());

		return $this->fetch();
	}

	/**
	 * 添加企业
	 * @adminMenu(
	 *     'name'   => '添加企业',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '添加企业',
	 *     'param'  => ''
	 * )
	 */
	public function add() {
		$themeModel = new ThemeModel();
		$articleThemeFiles = $themeModel->getActionThemeFiles('schain/Enterprise/index');
		$this->assign('article_theme_files', $articleThemeFiles);
		return $this->fetch();
	}

	/**
	 * 添加企业提交
	 * @adminMenu(
	 *     'name'   => '添加企业提交',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '添加企业提交',
	 *     'param'  => ''
	 * )
	 */
	public function addPost() {
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$post = $data['post'];
			$result = $this->validate($post, 'AdminEnterprise');
			if ($result !== true) {
				$this->error($result);
			}

			$schainEnterpriseModel = new SchainEnterpriseModel();

			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
				$data['post']['more']['photos'] = [];
				foreach ($data['photo_urls'] as $key => $url) {
					$photoUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
				}
			}

			if (!empty($data['file_names']) && !empty($data['file_urls'])) {
				$data['post']['more']['files'] = [];
				foreach ($data['file_urls'] as $key => $url) {
					$fileUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
				}
			}

			//后台添加名片时，作者是当前登录的管理员
			$data['post']['user_id'] = cmf_get_current_admin_id();

			$schainEnterpriseModel->adminAddEnterprise($data['post'], $data['post']['categories']);

			$data['post']['id'] = $schainEnterpriseModel->id;

			$this->success('添加成功!', url('AdminEnterprise/edit', ['id' => $schainEnterpriseModel->id]));
		}
	}

	/**
	 * 编辑企业
	 * @adminMenu(
	 *     'name'   => '编辑企业',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '编辑企业',
	 *     'param'  => ''
	 * )
	 */
	public function edit() {
		$id = $this->request->param('id', 0, 'intval');

		$schainEnterpriseModel = new SchainEnterpriseModel();
		$post = $schainEnterpriseModel->where('id', $id)->find();
		$postCategories = $post->categories()->alias('a')->column('a.name', 'a.id');
		$postCategoryIds = implode(',', array_keys($postCategories));

		$themeModel = new ThemeModel();
		$articleThemeFiles = $themeModel->getActionThemeFiles('schain/Enterprise/index');
		$this->assign('article_theme_files', $articleThemeFiles);
		$this->assign('post', $post);
		$this->assign('ent_categories', $postCategories);
		$this->assign('ent_category_ids', $postCategoryIds);

		return $this->fetch();
	}

	/**
	 * 编辑企业提交
	 * @adminMenu(
	 *     'name'   => '编辑企业提交',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '编辑企业提交',
	 *     'param'  => ''
	 * )
	 */
	public function editPost() {

		if ($this->request->isPost()) {
			$data = $this->request->param();
			$post = $data['post'];
			$result = $this->validate($post, 'AdminEnterprise');
			if ($result !== true) {
				$this->error($result);
			}

			$schainEnterpriseModel = new SchainEnterpriseModel();

			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
				$data['post']['more']['photos'] = [];
				foreach ($data['photo_urls'] as $key => $url) {
					$photoUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
				}
			}

			if (!empty($data['file_names']) && !empty($data['file_urls'])) {
				$data['post']['more']['files'] = [];
				foreach ($data['file_urls'] as $key => $url) {
					$fileUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
				}
			}

			$schainEnterpriseModel->adminEditEnterprise($data['post'], $data['post']['categories']);

			$hookParam = [
				'is_add' => false,
				'article' => $data['post'],
			];
			hook('schain_admin_after_save_article', $hookParam);

			$this->success('保存成功!');

		}
	}

	/**
	 * 企业删除
	 * @adminMenu(
	 *     'name'   => '企业删除',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业删除',
	 *     'param'  => ''
	 * )
	 */
	public function delete() {
		$param = $this->request->param();
		$schainEnterpriseModel = new SchainEnterpriseModel();

		if (isset($param['id'])) {
			$id = $this->request->param('id', 0, 'intval');
			$result = $schainEnterpriseModel->where(['id' => $id])->find();
			$data = [
				'object_id' => $result['id'],
				'create_time' => time(),
				'table_name' => 'schain_enterprise',
				'name' => $result['ent_title'],
				'user_id' => cmf_get_current_admin_id(),
			];
			$resultSchain = $schainEnterpriseModel
				->where(['id' => $id])
				->update(['delete_time' => time()]);
			if ($resultSchain) {
				Db::name('schain_category_enterprise')->where(['ent_id' => $id])->update(['status' => 0]);
				Db::name('schain_tag_enterprise')->where(['ent_id' => $id])->update(['status' => 0]);

				Db::name('recycleBin')->insert($data);
			}
			$this->success("删除成功！", '');

		}

		if (isset($param['ids'])) {
			$ids = $this->request->param('ids/a');
			$recycle = $schainEnterpriseModel->where(['id' => ['in', $ids]])->select();
			$result = $schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);
			if ($result) {
				Db::name('schain_category_enterprise')->where(['ent_id' => ['in', $ids]])->update(['status' => 0]);
				Db::name('schain_tag_enterprise')->where(['ent_id' => ['in', $ids]])->update(['status' => 0]);
				foreach ($recycle as $value) {
					$data = [
						'object_id' => $value['id'],
						'create_time' => time(),
						'table_name' => 'schain_enterprise',
						'name' => $value['ent_title'],
						'user_id' => cmf_get_current_admin_id(),
					];
					Db::name('recycleBin')->insert($data);
				}
				$this->success("删除成功！", '');
			}
		}
	}

	/**
	 * 企业发布
	 * @adminMenu(
	 *     'name'   => '企业发布',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业发布',
	 *     'param'  => ''
	 * )
	 */
	public function publish() {
		$param = $this->request->param();
		$schainEnterpriseModel = new SchainEnterpriseModel();

		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['ent_status' => 1, 'published_time' => time()]);

			$this->success("发布成功！", '');
		}

		if (isset($param['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['ent_status' => 0]);

			$this->success("取消发布成功！", '');
		}

	}

	/**
	 * 企业置顶
	 * @adminMenu(
	 *     'name'   => '企业置顶',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业置顶',
	 *     'param'  => ''
	 * )
	 */
	public function top() {
		$param = $this->request->param();
		$schainEnterpriseModel = new SchainEnterpriseModel();

		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['is_top' => 1]);

			$this->success("置顶成功！", '');

		}

		if (isset($_POST['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['is_top' => 0]);

			$this->success("取消置顶成功！", '');
		}
	}

	/**
	 * 企业推荐
	 * @adminMenu(
	 *     'name'   => '企业推荐',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业推荐',
	 *     'param'  => ''
	 * )
	 */
	public function recommend() {
		$param = $this->request->param();
		$schainEnterpriseModel = new SchainEnterpriseModel();

		if (isset($param['ids']) && isset($param["yes"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

			$this->success("推荐成功！", '');

		}
		if (isset($param['ids']) && isset($param["no"])) {
			$ids = $this->request->param('ids/a');

			$schainEnterpriseModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

			$this->success("取消推荐成功！", '');

		}
	}

	/**
	 * 企业排序
	 * @adminMenu(
	 *     'name'   => '企业排序',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业排序',
	 *     'param'  => ''
	 * )
	 */
	public function listOrder() {
		parent::listOrders(Db::name('schain_category_enterprise'));
		$this->success("排序更新成功！", '');
	}

	public function move() {

	}

	public function copy() {

	}

}
